import del from '@feidao/service/atom/db/del';
import query from '@feidao/service/atom/db/query';

interface Message {
	systemid1: string;
	systemid2: string;
	star: string;
}

export default async function atom(msg: Message, action_id: string, session_id: string, spaceid: string) {
	const { systemid1, systemid2 } = msg;
	const { exec: e1, prepare: p1 } = query(action_id, session_id, spaceid, systemid1);
	p1('star', ['_id'], {
		name: msg.star
	});
	const [stars] = await e1();
	if (stars.length !== 1) {
		return 'success';
	}
	const { exec, prepare } = del(action_id, session_id, spaceid, systemid2);
	// 3、确定要获取数据的表名，删除条件，若没有条件则全部删除
	prepare('works', [{ star: stars[0]._id }]);
	// 5、执行删除，删除结果为JS对象，code值为0、1,1标示执行成功，0标示执行失败，result为失败信息
	const ret = await exec()
	if (ret.code === 1) {
		return 'success';
	} else {
		return 'fail';
	}
}
